package cn.gcyl.module.biz.service.thirdpartyinformation;

import cn.gcyl.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.gcyl.module.biz.dal.dataobject.SysMemberUserDO;
import cn.gcyl.module.biz.dal.dataobject.useractionlog.UserActionLogDO;
import cn.gcyl.module.biz.dal.dataobject.usercolumnviewlog.UserColumnViewLogDO;
import cn.gcyl.module.biz.dal.mysql.SysMemberUserMapper;
import cn.gcyl.module.biz.dal.mysql.useractionlog.UserActionLogMapper;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.gcyl.module.biz.controller.admin.thirdpartyinformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.thirdpartyinformation.ThirdPartyInformationDO;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.util.object.BeanUtils;

import cn.gcyl.module.biz.dal.mysql.thirdpartyinformation.ThirdPartyInformationMapper;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.convertList;
import static cn.gcyl.framework.common.util.collection.CollectionUtils.diffList;
import static cn.gcyl.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.THIRD_PARTY_INFORMATION_NOT_EXISTS;

/**
 * 第三方信息管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ThirdPartyInformationServiceImpl implements ThirdPartyInformationService {

    @Resource
    private ThirdPartyInformationMapper thirdPartyInformationMapper;
    @Resource
    private UserActionLogMapper userActionLogMapper;
    @Resource
    private SysMemberUserMapper sysMemberUserMapper;


    @Override
    public Long createThirdPartyInformation(ThirdPartyInformationSaveReqVO createReqVO) {
        // 插入
        ThirdPartyInformationDO thirdPartyInformation = BeanUtils.toBean(createReqVO, ThirdPartyInformationDO.class);
        thirdPartyInformationMapper.insert(thirdPartyInformation);

        // 返回
        return thirdPartyInformation.getId();
    }
    @Override
    public Long createThirdPartyInformation(ThirdPartyInformationDO createReqVO) {
        // 插入
        ThirdPartyInformationDO thirdPartyInformation = BeanUtils.toBean(createReqVO, ThirdPartyInformationDO.class);
        thirdPartyInformationMapper.insert(thirdPartyInformation);

        // 返回
        return thirdPartyInformation.getId();
    }
    @Override
    public ThirdPartyInformationDO getThirdPartyInformationByThirdPartyId(String thirdPartyId) {
        return thirdPartyInformationMapper.selectOne(
                new LambdaQueryWrapper<ThirdPartyInformationDO>()
                        .eq(ThirdPartyInformationDO::getThirdPartyId, thirdPartyId)
        );
    }

    @Override
    public void updateThirdPartyInformation(ThirdPartyInformationSaveReqVO updateReqVO) {
        // 校验存在
        validateThirdPartyInformationExists(updateReqVO.getId());
        // 更新
        ThirdPartyInformationDO updateObj = BeanUtils.toBean(updateReqVO, ThirdPartyInformationDO.class);
        thirdPartyInformationMapper.updateById(updateObj);
    }

    @Override
    public void deleteThirdPartyInformation(Long id) {
        // 校验存在
        validateThirdPartyInformationExists(id);
        // 删除
        thirdPartyInformationMapper.deleteById(id);
    }

    @Override
        public void deleteThirdPartyInformationListByIds(List<Long> ids) {
        // 删除
        thirdPartyInformationMapper.deleteByIds(ids);
        }


    private void validateThirdPartyInformationExists(Long id) {
        if (thirdPartyInformationMapper.selectById(id) == null) {
            throw exception(THIRD_PARTY_INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public ThirdPartyInformationDO getThirdPartyInformation(Long id) {
        return thirdPartyInformationMapper.selectById(id);
    }
    @Override
    public ThirdPartyInformationDO getAppThirdPartyInformation(Long id) {
        ThirdPartyInformationDO thirdPartyInformationDO = thirdPartyInformationMapper.selectById(id);
        //添加操作日志
        Long loginUserId = getLoginUserId();
        UserActionLogDO userActionLogDO = new UserActionLogDO();
        userActionLogDO.setUserId(loginUserId);
        userActionLogDO.setAction("访问三方信息:"+thirdPartyInformationDO.getTitle()+"，标题类型："+thirdPartyInformationDO.getInfoType());
        userActionLogMapper.insert(userActionLogDO);
        //非会员空户只能查看一次,添加查看记录
        SysMemberUserDO sysMemberUserDO = sysMemberUserMapper.selectById(loginUserId);
        sysMemberUserDO.setViewingTime(LocalDateTime.now());
        sysMemberUserMapper.updateById(sysMemberUserDO);
        return thirdPartyInformationDO;
    }

    @Override
    public PageResult<ThirdPartyInformationDO> getThirdPartyInformationPage(ThirdPartyInformationPageReqVO pageReqVO) {
        return thirdPartyInformationMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<ThirdPartyInformationDO> getAppThirdPartyInformationPage(AppThirdPartyInformationPageReqVO pageReqVO) {
        return thirdPartyInformationMapper.getAppThirdPartyInformationPage(pageReqVO);
    }

}